package com.lzx.article.controller.web;

import com.lzx.article.entity.Articles;
import com.lzx.article.mapper.ArticlesMapper;
import com.lzx.article.service.ArticlesService;
import com.lzx.article.service.BrowseService;
import com.lzx.article.vo.ArticleVo;
import com.lzx.article.vo.ArticleVos;
import com.lzx.common.utils.R;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/article/articles/web")
public class WebArticlesController {
    @Resource
    private ArticlesService articlesService;

    @Resource
    private BrowseService browseService;

    @Resource
    private ArticlesMapper articlesMapper;

    //按年份统计每月文章发布数量
    @GetMapping("/countByMonth/{year}")
    public R countArticlesByMonthOfYear(@PathVariable int year) {
        List<Map<String, Object>> list = articlesMapper.countArticlesByMonthOfYear(year);
        return R.ok().data("list", list);
    }

    //获取浏览量最多的十篇文章
    @GetMapping("/getTopTenArticle")
    public R getTopTenArticle() {
        return articlesService.getTopTenArticle();
    }

    //获取文章总数
    @GetMapping("/getArticleTotal")
    public Map<String, Integer> getArticleTotal() {
        Map<String, Integer> map = new HashMap<>();
        map.put("browseNum", Math.toIntExact(browseService.count()));
        map.put("articleNum", Math.toIntExact(articlesService.count()));
        return map;
    }

    //用户修改头像或昵称 es同步
    @GetMapping("/getUserArticleListToES/{userId}")
    public List<Articles> getUserArticleListToES(@PathVariable Long userId) {
        return articlesService.getUserArticleListToES(userId);
    }

    //查询文章（提供给其他服务）
    @GetMapping("/getArticleToOther/{id}")
    public Articles getArticleToOther(@PathVariable Long id) {
        return articlesService.getArticleToOther(id);
    }

    //获取所有文章
    @GetMapping("/getArticleListToES")
    public List<Articles> getArticleListToES() {
        return articlesService.getArticleListToES();
    }

    //获取当前用户发布的文章
    @GetMapping("/getUserArticleList/{userId}")
    public R getUserArticleList(@PathVariable Long userId) {
        return articlesService.getUserArticleList(userId);
    }

    //查询当前用户是否点赞或收藏了文章
    @GetMapping("/getUserIsLikeOrCollectArticle/{articleId}/{userId}")
    public R getUserIsLikeOrCollectArticle(@PathVariable int articleId, @PathVariable int userId) {
        return articlesService.getUserIsLikeOrCollectArticle(articleId, userId);
    }

    //获取文章信息
    @GetMapping("/getArticleDetails/{id}")
    public R getArticleDetails(@PathVariable Long id) {
        return articlesService.getArticleDetails(id);
    }

    //获取文章列表
    @GetMapping("/getArticleList/{current}/{size}")
    public R getArticleList(@PathVariable int current, @PathVariable int size) {
        return articlesService.getArticleList(current, size);
    }

    //发布文章
    @PostMapping("/saveArticle")
    public R saveArticle(@RequestBody ArticleVo articleVo) {
        return articlesService.saveArticle(articleVo);
    }

    //删除文章
    @DeleteMapping("/deleteArticle/{ids}")
    public R deleteArticle(@PathVariable List<Integer> ids) {
        return articlesService.deleteArticle(ids);
    }

    //修改文章
    @PutMapping("/modifyArticle")
    public R modifyArticle(@RequestBody ArticleVos articleVos) {
        return articlesService.modifyArticle(articleVos);
    }
}
